package com.yulongtian.offers.day202211.day20221106;

/**
 * https://leetcode.cn/problems/1nzheng-shu-zhong-1chu-xian-de-ci-shu-lcof/?favorite=xb9nqhhg
 * 可
 *
 * @author yulongTian
 * @create 2022-11-06 9:55
 */
public class Test02 {
    public static void main(String[] args) {
        System.out.println(countDigitOne(1410065408));
    }

    public static int countDigitOne(int n) {
        if (n == 1) {
            return n;
        }

        int base = 1;
        int res = 0;
        while (base <= n) {
            int a = n / base / 10;
            int b = n % base;
            int cur = (n / base) % 10;

            if (cur == 1) {
                res += a * base + b + 1;
            }

            if (cur == 0) {
                res += a * base;
            }

            if (cur > 1) {
                res += (a + 1) * base;
            }
            if (base == 1000000000) {
                break;
            }
            base *= 10;
        }
        return res;
    }
}
